#! /bin/bash
# ----------------------------------------------------------------------------
#
# Init a chia plot system and start plotting.
#
# Applicable on Ubuntu system only.
#
# yqm_leaf@163.com
#
# 05/02/21
#
# ----------------------------------------------------------------------------


CHIA_CMD_FILE="/usr/lib/chia-blockchain/resources/app.asar.unpacked/daemon/chia"
BIN_FILE_URL="https://github.com/Chia-Network/chia-blockchain/releases/download/1.1.3/chia-blockchain_1.1.3_amd64.deb"

[ $UID -ne 0 ] && { echo "Please run this script as root." ; exit 1 ; }

type apt-get > /dev/null 2>&1 || \
    { echo "This script is applicable on Ubuntu system only" ; exit 1 ; }

type lshw > /dev/null 2>&1 || apt-get install -y lshw || \
    { echo "install lshw failed" ; exit 1 ; }

type wget > /dev/null 2>&1 || apt-get install -y wget || \
    { echo "install wget failed" ; exit 1 ; }

timedatectl set-ntp true

echo "+++++++++++++CPU info++++++++++++"
lscpu
echo "+++++++++++++RAM info++++++++++++"
lshw -class memory
free -h
echo "+++++++++++++Network info++++++++++++"
lshw -class network | grep "name"

wget "${BIN_FILE_URL}" || \
    { echo "error downloading chia binary package" ; exit 1 ; }
dpkg -i $(basename ${BIN_FILE_URL}) || \
    { echo "error installing chia" ; exit 1 ; }

# create soft link for 'chia' command.
type chia > /dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo "'chia' command not in PATH, creating soft links"
    # Ubuntu based.
    [ -f "${UBUNTU_FILE}" ] && \
        sudo ln -sf ${UBUNTU_FILE} ${BIN_PATH}
    # Arch linux based.
    [ -f "${ARCH_FILE}" ] && \
        sudo ln -sf ${ARCH_FILE} ${BIN_PATH}
fi

type chia > /dev/null 2>&1 || \
    { echo "Failed to find chia on the system, is it installed?" ; exit 1 ; }

echo "+++++++++++++Disk usage++++++++++++"
df -h
